/*
 * 
 */
package com.testrepper.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.repper.entity.ExamBundle;
import com.repper.entity.QandAnswer;

/**
 * The Class ExamServiceImpl.
 */
public class ExamServiceImpl implements ExamService {

	/** The Constant ILLEGAL_INPUT_TYPE_NUM_QUESTIONS. */
	private static final String ILLEGAL_INPUT_TYPE_NUM_QUESTIONS = "Type must be inputed and number of questions must be greater than 0";

	/** The doc service. */
	private DocumentService docService;

	/**
	 * Hey Mike Yockey! When calling ensure the bundle.getExamLocation is not
	 * NULL!
	 * 
	 * @param type
	 *            the type
	 * @param numQuestions
	 *            the num questions
	 * @return the exam bundle
	 */
	public ExamBundle createExamBundle(String type, int numQuestions) {
		if (type == null || numQuestions <= 0)
			throw new IllegalArgumentException(ILLEGAL_INPUT_TYPE_NUM_QUESTIONS);
		ExamBundle bundle = new ExamBundle();
		Long id = generateID();
		bundle.setId(id);
		// generate q & a
		List<QandAnswer> qNa = generateQuestionsAndAnswers(EXAM_TYPE_ADDITION, numQuestions);
		// call documentService to create documents
		try {
			String examPath = getDocService().generateExamDocuments(qNa, id);
			bundle.setExamLocaion(examPath);
		} catch (IOException e) {

		}
		return bundle;
	}

	// TODO: create actual implementation when db is available
	/**
	 * Generate id.
	 * 
	 * @return the long
	 */
	private Long generateID() {
		Random rand = new Random();
		return Math.abs(rand.nextLong());
	}

	/**
	 * Generate questions and answers.
	 * 
	 * @param examTypeAddition
	 *            the exam type addition
	 * @param numQuestions
	 *            the num questions
	 * @return the list
	 */
	public List<QandAnswer> generateQuestionsAndAnswers(String examTypeAddition, int numQuestions) {
		List<QandAnswer> results = new ArrayList<QandAnswer>();
		Random r = new Random();
		for (int i = 0; i < numQuestions; i++) {
			int arg1 = r.nextInt(10);
			int arg2 = r.nextInt(10);
			int id = i + 1;
			Map<String, Integer> choices = new HashMap<String, Integer>();
			choices.put("A", new Integer(arg1 + arg2));
			choices.put("B", new Integer(arg1 + arg2 + 1));
			choices.put("C", new Integer(arg1 + arg2 + 2));
			choices.put("D", new Integer(arg1 + arg2 + 3));
			choices.put("E", new Integer(arg1 + arg2 + 4));
			QandAnswer qa = new QandAnswer();
			qa.setAnswer("A");
			qa.setAnswerChoices(choices);
			qa.setId(id);
			qa.setNumOne(arg1);
			qa.setNumTwo(arg2);
			results.add(qa);
		}
		return results;
	}

	/**
	 * Gets the doc service.
	 * 
	 * @return the doc service
	 */
	public DocumentService getDocService() {
		if (docService == null)
			docService = new DocumentServiceImpl();
		return docService;
	}

	/**
	 * Sets the doc service.
	 * 
	 * @param docService
	 *            the new doc service
	 */
	public void setDocService(DocumentService docService) {
		this.docService = docService;
	}

}
